# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/7/10
# @File   : IC11_Past6MonsAvgRepaymentPro.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:最近6月平均应还款比例
code:IC11_Past6MonsAvgRepaymentPro
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def IC11_Past6MonsAvgRepaymentPro():
    try:
        ICRUndestoryLoancard = getTable('ICRUndestoryLoancard')
        ICRUndestoryStandardLoancard = getTable('ICRUndestoryStandardLoancard')
        ICRUnpaidLoan = getTable('ICRUnpaidLoan')

        if ICRUndestoryStandardLoancard is None and ICRUndestoryLoancard is None and ICRUnpaidLoan is None:
            return u'缺失值'
        elif ICRUndestoryStandardLoancard == [{}] and ICRUndestoryLoancard == [{}] and ICRUnpaidLoan == [{}]:
            return 0
        else:
            std_6usedAvgAmount = ICRUndestoryStandardLoancard[0].get('latest6MonthUsedAvgAmount')
            std_creditLimit = ICRUndestoryStandardLoancard[0].get('creditLimit')
            if isfloat(std_6usedAvgAmount):
                std_6usedAvgAmount = float(std_6usedAvgAmount)
            else:
                std_6usedAvgAmount = None
            if isfloat(std_creditLimit):
                std_creditLimit = float(std_creditLimit)
            else:
                std_creditLimit = None

            card_6usedAvgAmount = ICRUndestoryLoancard[0].get('latest6MonthUsedAvgAmount')
            card_creditLimit = ICRUndestoryLoancard[0].get('creditLimit')
            if isfloat(card_6usedAvgAmount):
                card_6usedAvgAmount = float(card_6usedAvgAmount)
            else:
                card_6usedAvgAmount = None
            if isfloat(card_creditLimit):
                card_creditLimit = float(card_creditLimit)
            else:
                card_creditLimit = None

            loan_6usedAvgAmount = ICRUnpaidLoan[0].get('latest6MonthUsedAvgAmount')
            loan_creditLimit = ICRUnpaidLoan[0].get('creditLimit')
            if isfloat(loan_6usedAvgAmount):
                loan_6usedAvgAmount = float(loan_6usedAvgAmount)
            else:
                loan_6usedAvgAmount = None
            if isfloat(loan_creditLimit):
                loan_creditLimit = float(loan_creditLimit)
            else:
                loan_creditLimit = None


            if std_6usedAvgAmount is None and card_6usedAvgAmount is None and loan_6usedAvgAmount is None:
                return u'缺失值'
            else:
                std_6usedAvgAmount = std_6usedAvgAmount if std_6usedAvgAmount !=None else 0
                card_6usedAvgAmount = card_6usedAvgAmount if card_6usedAvgAmount != None else 0
                loan_6usedAvgAmount = loan_6usedAvgAmount if loan_6usedAvgAmount != None else 0

            if std_creditLimit is None and card_creditLimit is None and loan_creditLimit is None:
                return u'缺失值'
            else:
                std_creditLimit = std_creditLimit if std_creditLimit != None else 0
                card_creditLimit = card_creditLimit if card_creditLimit != None else 0
                loan_creditLimit = loan_creditLimit if loan_creditLimit != None else 0

            result = (std_6usedAvgAmount + card_6usedAvgAmount + loan_6usedAvgAmount) / (std_creditLimit + card_creditLimit + loan_creditLimit)

            return round(result, 4)
    except:
        return u'缺失值'


result = IC11_Past6MonsAvgRepaymentPro()
